﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using TLL.Finance.BankOrg;
using TLL.Finance.Currencys;
using TLL.UTL;
using TasksLog.Conf;

namespace TasksLog
{
    public partial class OrganizationsForm : Form
    {
        private bool MouseClick = false;
        private Loader Loader = null;
        private Point MouseClickPosition;
        private List<Organization> organizations = null;
        private CurrencyType OrderType = CurrencyType.USD;
        public OrganizationsForm()
        {
            InitializeComponent();
            InitializeData();
            this.StartPosition = FormStartPosition.CenterScreen;
            CBOrder.SelectedIndex = 0;
            CBOrderBy.SelectedIndex = 0;
        }
        private void InitializeData()
        {
            if (Loader == null)
                Loader = new Loader(this, LoaderType.Diagram);

            Loader.Start();

            using (var repository = new OrganizationCurrencysRepository(null))
            {
                if (MainConf.Organizations.Count == 0)
                {
                    this.organizations = repository.Get();
                    MainConf.Organizations = this.organizations;
                }
                else
                    this.organizations = MainConf.Organizations;

                var OrganizationWithUSD = this.organizations
                    .Where(x=>x.Currencys.FirstOrDefault(y=>y.Type== CurrencyType.USD) != null)
                    .OrderBy(x=>x.Currencys.FirstOrDefault(y=>y.Type == CurrencyType.USD).Buy)
                    .Reverse();

                var OrganizationWithEUR = this.organizations
                    .Where(x => x.Currencys.FirstOrDefault(y => y.Type == CurrencyType.EUR) != null)
                    .OrderBy(x => x.Currencys.FirstOrDefault(y => y.Type == CurrencyType.EUR).Buy)
                    .Reverse();

                var OrganizationWithRUB = this.organizations
                    .Where(x => x.Currencys.FirstOrDefault(y => y.Type == CurrencyType.RUB) != null)
                    .OrderBy(x => x.Currencys.FirstOrDefault(y => y.Type == CurrencyType.RUB).Buy)
                    .Reverse();

                LbRatingMaxUSD.Text = LbRatingMaxUSD.Text
                    .Replace("{0}", MainConf.Currencys.FirstOrDefault(x=> x.Country == CurrencyCountry.Ukraine && x.Type == CurrencyType.USD).MaxRate.ToString("0.00"));

                LbRatingMaxEUR.Text = LbRatingMaxEUR.Text
                    .Replace("{0}", MainConf.Currencys.FirstOrDefault(x => x.Country == CurrencyCountry.Ukraine && x.Type == CurrencyType.EUR).MaxRate.ToString("0.00"));

                LbRatingMaxRUB.Text = LbRatingMaxRUB.Text
                    .Replace("{0}", MainConf.Currencys.FirstOrDefault(x => x.Country == CurrencyCountry.Ukraine && x.Type == CurrencyType.RUB).MaxRate.ToString("0.00"));

                LbRatingMinUSD.Text = LbRatingMinUSD.Text
                    .Replace("{0}", MainConf.Currencys.FirstOrDefault(x => x.Country == CurrencyCountry.Ukraine && x.Type == CurrencyType.USD).MinRate.ToString("0.00"));

                LbRatingMinEUR.Text = LbRatingMinEUR.Text
                    .Replace("{0}", MainConf.Currencys.FirstOrDefault(x => x.Country == CurrencyCountry.Ukraine && x.Type == CurrencyType.EUR).MinRate.ToString("0.00"));

                LbRatingMinRUB.Text = LbRatingMinRUB.Text
                    .Replace("{0}", MainConf.Currencys.FirstOrDefault(x => x.Country == CurrencyCountry.Ukraine && x.Type == CurrencyType.RUB).MinRate.ToString("0.00"));

                LBName.DataSource = this.organizations;
                LBRegion.DataSource = this.organizations.Select(x => x.Region).ToList();
                LBCity.DataSource = this.organizations.Select(x => x.City).ToList();
                LBAddress.DataSource = this.organizations.Select(x => x.Adress).ToList();
                LBUSD.DataSource = this.organizations.Select(x => Currency.Formated(x.Currencys.FirstOrDefault(y => y.Type == CurrencyType.USD))).ToList();
                LBEUR.DataSource = this.organizations.Select(x => Currency.Formated(x.Currencys.FirstOrDefault(y => y.Type == CurrencyType.EUR))).ToList();
                LBRUB.DataSource = this.organizations.Select(x => Currency.Formated(x.Currencys.FirstOrDefault(y => y.Type == CurrencyType.RUB))).ToList();

                CBRegions.DataSource = repository.GetRegions();
                CBCities.DataSource = repository.GetCitys();

            }
            this.WindowState = FormWindowState.Normal;
            Loader.Stop();
        }

        private void CBRegions_SelectedValueChanged(object sender, EventArgs e)
        {
            if (CBRegions.SelectedItem != null)
            {
                var collections = this.organizations.Where(x => x.Region.ID == CBRegions.SelectedValue).ToList();

                collections = collections
                    .Where(x => x.Currencys.FirstOrDefault(y => y.Type == this.OrderType) != null)
                    .OrderBy(x => x.Currencys.FirstOrDefault(y => y.Type == this.OrderType).Buy).Reverse().Take(25).ToList();

                LBName.DataSource = collections;
                LBRegion.DataSource = collections.Select(x => x.Region).ToList();
                LBCity.DataSource = collections.Select(x => x.City).ToList();
                LBAddress.DataSource = collections.Select(x => x.Adress).ToList();
                LBUSD.DataSource = collections.Select(x => Currency.Formated(x.Currencys.FirstOrDefault(y => y.Type == CurrencyType.USD))).ToList();
                LBEUR.DataSource = collections.Select(x => Currency.Formated(x.Currencys.FirstOrDefault(y => y.Type == CurrencyType.EUR))).ToList();
                LBRUB.DataSource = collections.Select(x => Currency.Formated(x.Currencys.FirstOrDefault(y => y.Type == CurrencyType.RUB))).ToList();
            }
        }
        private void Clear()
        {
            LBName.Items.Clear();
            LBAddress.Items.Clear();
            LBUSD.Items.Clear();
            LBEUR.Items.Clear();
            LBRUB.Items.Clear();
        }

        private void CBCities_SelectedValueChanged(object sender, EventArgs e)
        {
            if (CBRegions.SelectedItem != null)
            {
                var collections = this.organizations.Where(x => x.City.ID == CBCities.SelectedValue).ToList();

                collections = collections
                    .Where(x => x.Currencys.FirstOrDefault(y => y.Type == this.OrderType) != null)
                    .OrderBy(x => x.Currencys.FirstOrDefault(y => y.Type == this.OrderType).Buy).Reverse().Take(25).ToList();

                LBName.DataSource = collections;
                LBRegion.DataSource = collections.Select(x => x.Region).ToList();
                LBCity.DataSource = collections.Select(x => x.City).ToList();
                LBAddress.DataSource = collections.Select(x => x.Adress).ToList();
                LBUSD.DataSource = collections.Select(x => Currency.Formated(x.Currencys.FirstOrDefault(y => y.Type == CurrencyType.USD))).ToList();
                LBEUR.DataSource = collections.Select(x => Currency.Formated(x.Currencys.FirstOrDefault(y => y.Type == CurrencyType.EUR))).ToList();
                LBRUB.DataSource = collections.Select(x => Currency.Formated(x.Currencys.FirstOrDefault(y => y.Type == CurrencyType.RUB))).ToList();
            }
        }

        private void LB_SelectedIndexChanged(object sender, EventArgs e)
        {
            var LB = sender as ListBox;

            if (LBAddress.Items.Count != 0)
            {
                LBName.SelectedIndex = LB.SelectedIndex;
                LBAddress.SelectedIndex = LB.SelectedIndex;
                LBRegion.SelectedIndex = LB.SelectedIndex;
                LBCity.SelectedIndex = LB.SelectedIndex;
                LBUSD.SelectedIndex = LB.SelectedIndex;
                LBEUR.SelectedIndex = LB.SelectedIndex;
                LBRUB.SelectedIndex = LB.SelectedIndex;
            }
        }

        private void CBOrder_SelectedValueChanged(object sender, EventArgs e)
        {
            this.OrderType = Utilities.GetEnum<CurrencyType>(CBOrder.SelectedItem as string);
            if (CBRegions.SelectedItem != null)
            {
                var collections = new List<Organization>();
                switch (CBOrderBy.SelectedIndex)
                {
                    case 0: collections = this.organizations.Where(x => x.City.ID == CBCities.SelectedValue).ToList();break;
                    case 1: collections = this.organizations.Where(x => x.Region.ID == CBRegions.SelectedValue).ToList();break;
                }

                collections = collections
                    .Where(x => x.Currencys.FirstOrDefault(y => y.Type == this.OrderType) != null)
                    .OrderBy(x => x.Currencys.FirstOrDefault(y => y.Type == this.OrderType).Buy).Reverse().Take(25).ToList();

                LBName.DataSource = collections;
                LBRegion.DataSource = collections.Select(x => x.Region).ToList();
                LBCity.DataSource = collections.Select(x => x.City).ToList();
                LBAddress.DataSource = collections.Select(x => x.Adress).ToList();
                LBUSD.DataSource = collections.Select(x => Currency.Formated(x.Currencys.FirstOrDefault(y => y.Type == CurrencyType.USD))).ToList();
                LBEUR.DataSource = collections.Select(x => Currency.Formated(x.Currencys.FirstOrDefault(y => y.Type == CurrencyType.EUR))).ToList();
                LBRUB.DataSource = collections.Select(x => Currency.Formated(x.Currencys.FirstOrDefault(y => y.Type == CurrencyType.RUB))).ToList();
            }
        }

        private void CBOrderBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch(CBOrderBy.SelectedIndex)
            {
                case 0: 
                    CBCities.Visible = true;
                    LbCities.Visible = true;
                    CBRegions.Visible = false;
                    LbRegions.Visible = false;
                    break;
                case 1:
                    CBCities.Visible = false;
                    LbCities.Visible = false;
                    CBRegions.Visible = true;
                    LbRegions.Visible = true;
                    break;
            }
        }

        private void OrganizationsForm_Load(object sender, EventArgs e)
        {

        }

        private void BtClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BtInDock_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void panel1_DragDrop(object sender, DragEventArgs e)
        {
            var asd = 123;
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            
            this.MouseClick = true;
            this.MouseClickPosition = new Point(e.Location.X, e.Location.Y);
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            this.MouseClick = false;
            this.MouseClickPosition = Point.Empty;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.MouseClick)
            {
                this.Top = MousePosition.Y - this.MouseClickPosition.Y;
                this.Left = MousePosition.X - this.MouseClickPosition.X;
            }
        }
    }
}
